﻿using System;
using System.Text.RegularExpressions;

namespace NwProvider
{
    internal static class HtmlParser
    {
        internal static string GetAuthor(string text)
        {
            return GetAttribute(text, "nw_author");
        }

        internal static DateTime GetDate(string text)
        {
            var str = GetAttribute(text, "nw_pub_date");

            if (string.IsNullOrEmpty(str))
                return DateTime.Now;

            try
            {
                return Convert.ToDateTime(str);
            }
            catch
            {
                return DateTime.Now;
            }
        }

        internal static string GetUrl(string text)
        {
            var attrib = "fbLike(escape(\"";
            var endAttrib = "\"),";

            return GetText(text, attrib, endAttrib);
        }

        internal static string GetTitle(string text)
        {
            var attrib = "<title>";
            var endAttrib = " - Newsweek";

            return GetText(text, attrib, endAttrib);
        }

        internal static string GetArticle(string text)
        {
            var attrib = "<div class=\"text\"><p>";
            var endAttrib = "</article>";

            var newText = GetText(text, attrib, endAttrib);

            var patterm = @"<.*?>";

            var outText = Regex.Replace(newText, patterm, "");

            return outText.Replace('\n', ' ');
        }

        internal static string GetText(string text, string attrib, string endAttrib)
        {
            int initIndex = 0;
            return GetText(text, attrib, endAttrib, ref initIndex);
        }

        internal static string GetText(string text, string attrib, string endAttrib, ref int startIndex)
        {
            startIndex = text.IndexOf(attrib, startIndex);

            if (startIndex == -1)
                return string.Empty;

            startIndex += attrib.Length;

            var endIndex = text.IndexOf(endAttrib, startIndex + 1);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex - 1;

            if (length <= 0)
                return string.Empty;

            return text.Substring(startIndex, length);
        }

        private static string GetAttribute(string text, string attr)
        {
            var index = text.IndexOf(attr);

            if (index == -1)
                return string.Empty;

            var startIndex = text.IndexOf('\"', index);

            if (startIndex == -1)
                return string.Empty;

            var endIndex = text.IndexOf('\"', startIndex + 1);

            if (endIndex == -1)
                return string.Empty;

            var length = endIndex - startIndex - 1;

            if (length <= 0)
                return string.Empty;

            return text.Substring(startIndex + 1, length);
        }
    }
}
